<template>
  <div class="workflow-list" v-loading="loading">
    <el-empty
      v-if="!workflows.length"
      description="暂无工作流"
      class="empty-tip"
    />
    <el-row v-else :gutter="24">
      <el-col
        v-for="workflow in workflows"
        :key="workflow.id"
        :xs="24"
        :sm="12"
        :md="8"
        :lg="6"
      >
        <el-card class="workflow-card" :body-style="{ padding: '0' }">
          <div class="cover">
            <img :src="workflow.cover" :alt="workflow.title" />
            <div class="actions">
              <el-button-group>
                <el-button
                  type="primary"
                  :icon="CaretRight"
                  @click="$emit('execute', workflow)"
                >
                  执行
                </el-button>
                <el-button
                  v-if="canManage(workflow)"
                  type="primary"
                  :icon="Edit"
                  @click="$emit('edit', workflow)"
                >
                  编辑
                </el-button>
                <el-button
                  v-if="canManage(workflow)"
                  type="danger"
                  :icon="Delete"
                  @click="$emit('delete', workflow)"
                >
                  删除
                </el-button>
              </el-button-group>
            </div>
          </div>

          <div class="info">
            <h3 class="title" :title="workflow.title">
              {{ workflow.title }}
              <el-tag v-if="workflow.isPublic" size="small" type="success">
                公开
              </el-tag>
            </h3>
            <p class="description" :title="workflow.description">
              {{ workflow.description || '暂无描述' }}
            </p>
            <div class="meta">
              <div class="tags">
                <el-tag
                  v-for="tag in workflow.tags"
                  :key="tag"
                  size="small"
                  effect="plain"
                >
                  {{ tag }}
                </el-tag>
              </div>
              <span class="time">
                {{ formatDate(workflow.updatedAt) }}
              </span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { CaretRight, Edit, Delete } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import dayjs from 'dayjs'
import type { Workflow } from '@/types/workflow'

const userStore = useUserStore()

defineProps<{
  workflows: Workflow[]
  loading?: boolean
}>()

defineEmits<{
  (e: 'execute', workflow: Workflow): void
  (e: 'edit', workflow: Workflow): void
  (e: 'delete', workflow: Workflow): void
}>()

// 检查是否可以管理工作流
const canManage = (workflow: Workflow) => {
  return userStore.isAdmin() || userStore.isOwner(workflow.userId)
}

// 格式化日期
const formatDate = (date: string | Date) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm')
}
</script>

<style scoped lang="scss">
.workflow-list {
  .workflow-card {
    margin-bottom: 24px;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-4px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }

    .cover {
      position: relative;
      height: 200px;
      overflow: hidden;

      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      .actions {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        padding: 16px;
        background: linear-gradient(
          to bottom,
          transparent,
          rgba(0, 0, 0, 0.6)
        );
        text-align: center;
      }
    }

    .info {
      padding: 16px;

      .title {
        margin: 0 0 8px;
        font-size: 16px;
        font-weight: 500;
        line-height: 1.4;
        @include text-overflow;

        .el-tag {
          margin-left: 8px;
          vertical-align: middle;
        }
      }

      .description {
        margin: 0 0 12px;
        color: #666;
        font-size: 14px;
        line-height: 1.5;
        @include multi-line-overflow(2);
      }

      .meta {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .tags {
          flex: 1;
          margin-right: 16px;
          overflow: hidden;

          .el-tag {
            margin: 0 8px 4px 0;
          }
        }

        .time {
          flex-shrink: 0;
          color: #999;
          font-size: 12px;
        }
      }
    }
  }

  .empty-tip {
    padding: 32px;
  }
}
</style> 